package leetcode.L739;

import java.util.Deque;
import java.util.LinkedList;

class Solution {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] ans = new int[temperatures.length];
        Deque<Integer> que = new LinkedList<>();
        que.push(0);
        for (int i = 0; i < temperatures.length; i++) {
            if (temperatures[que.peek()] >= temperatures[i]) {
                que.push(i);
            } else {
                while (!que.isEmpty() && temperatures[que.peek()] < temperatures[i]) {
                    ans[que.peek()] = i - que.pop();
                }
                que.push(i);
            }
        }
        return ans;
    }
}